#include "../inc/Chess/horse.h"
#include <cmath>
#include <iostream>
using namespace std;

Horse::Horse(PieceType type, int row, int col, bool isRed)
    : Piece(type, row, col, isRed), isHorseLegBlocked(false)
{
}

bool Horse::is_valid_move(int to_row, int to_col, PieceType board[9][10])
{

    int row = position.first;
    int col = position.second;

    // 计算移动距离
    int row_diff = to_row - row;
    int col_diff = to_col - col;

    // 马走"日"字形：横向走2格纵向走1格，或横向走1格纵向走2格
    if (!((abs(row_diff) == 2 && abs(col_diff) == 1) ||
          (abs(row_diff) == 1 && abs(col_diff) == 2)))
    {
        return false;
    }

    // 检查是否蹩马腿
    // 横向走2格纵向走1格的情况
    if (abs(row_diff) == 2 && abs(col_diff) == 1)
    {
        // 检查马腿位置是否有棋子（起始位置与目标位置之间的位置）
        int leg_row = row + row_diff / 2; 
        int leg_col = col;               

        if (board[leg_row][leg_col] != PieceType::kong)
        {
            cout << "row" << row << "col" << col << endl;
            cout << "to_row  " << to_row << "to_col  " << to_col << endl;
            cout << "leg_row  " << leg_row << "leg_col  " << leg_col << endl;
            cout << "蹩马腿" << endl;
            return false;
        }
    }
    // 横向走1格纵向走2格的情况
    else if (abs(row_diff) == 1 && abs(col_diff) == 2)
    {
        // 检查马腿位置是否有棋子
        int leg_row = row;                
        int leg_col = col + col_diff / 2; 

        if (board[leg_row][leg_col] != PieceType::kong)
        {
            cout << "row" << row << "col" << col << endl;
            cout << "to_row  " << to_row << "to_col  " << to_col << endl;
            cout << "leg_row  " << leg_row << "leg_col  " << leg_col << endl;
            cout << "蹩马腿" << endl;
            return false;
        }
    }
    return true;
}